package com.css.fxfzypg.manage.disasterrisk.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.attachment.entity.AttachmentInfoEntity;
import com.css.fxfzypg.base.attachment.service.AttachmentInfoService;
import com.css.fxfzypg.base.exception.ServiceException;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.logInfo.SysLog;
import com.css.fxfzypg.manage.disasterrisk.repository.entity.DisasterRiskEntity;
import com.css.fxfzypg.manage.disasterrisk.service.DisasterRiskService;
import com.css.fxfzypg.manage.disasterrisk.vo.DisasterRiskParams;
import com.css.fxfzypg.util.PlatformObjectUtils;
import com.css.fxfzypg.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/disasterRisk")
@Slf4j
public class DisasterRiskController {
    @Autowired
    DisasterRiskService disasterRiskService;
    @Autowired
    AttachmentInfoService attachmentInfoService;
    @Autowired
    HttpServletRequest request;

    /**
     * 判断该调研点在哪个任务区域中,根据经纬度去判断
     * @param x
     * @param y
     * @return
     */
  /* @GetMapping("/checkPoint")
    public RestResponse checkPoint(String x,String y) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{

            Map<String, Object> map = disasterRiskService.checkPoint(x, y);
            response = RestResponse.succeed(map);
        }catch (Exception e){
            log.error("风险点和区域校验失败!",e);
            json.put("message", "风险点和区域校验失败!");
            response=RestResponse.succeed(json);
        }

        return response;
    }*/

    /**
     * 新增数据
     *
     * @param disasterRiskEntity
     * @return
     */
    @SysLog("新增灾害风险点")
    @PostMapping("/insertDisasterRisk")
    public RestResponse insertDisasterRisk(@RequestBody DisasterRiskEntity disasterRiskEntity) {
        RestResponse response = null;
        try {
            boolean res = disasterRiskService.saveDisasterRisk(disasterRiskEntity);
            if (res) {
                response = RestResponse.succeed("保存成功!");
                response.setMessage("保存成功!");
            } else {
                response = RestResponse.fail("保存失败!");
            }
        } catch (Exception e) {
            String errormessage = "保存失败!";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 保存并提交
     *
     * @param disasterRiskEntity
     * @return
     */
    @SysLog("保存并提交灾害风险点")
    @PostMapping("/saveAndSubmit")
    public RestResponse saveAndSubmit(@RequestBody DisasterRiskEntity disasterRiskEntity) {
        RestResponse response = null;
        try {
            boolean res = disasterRiskService.saveAndSubmit(disasterRiskEntity);
            if (res) {
                response = RestResponse.succeed("保存并提交成功!");
                response.setMessage("保存并提交成功!");
            } else {
                response = RestResponse.fail("保存并提交失败!");
            }
        } catch (Exception e) {
            String errormessage = "保存并提交失败!";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 查询已提交和未提交列表
     *
     * @param request
     * @param submitFlag
     * @param userId
     * @return
     */
    @GetMapping("/querySubmitOrNot")
    public RestResponse querySubmitOrNot(HttpServletRequest request, String submitFlag, String userId) {
        RestResponse response = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> map = disasterRiskService.querySubmitOrNot(submitFlag, curPage, pageSize, userId);
            response = RestResponse.succeed(map);
        } catch (Exception e) {
            log.error("获取列表信息失败!", e);
            response = RestResponse.fail("获取列表信息失败!");
        }
        return response;
    }

    /**
     * 根据id更新提交状态
     *
     * @param map
     * @return
     */
    @SysLog("提交灾害风险点")
    @PostMapping("/updateIsSubmit")
    public RestResponse updateIsSubmit(@RequestBody Map<String, String> map) {
        RestResponse response = null;
        try {
            boolean res = disasterRiskService.updateIsSubmit(map);
            if (res) {
                response = RestResponse.succeed("提交成功!");
                response.setMessage("提交成功!");
            } else {
                response = RestResponse.fail("提交失败!");
            }
        } catch (Exception e) {
            String errormessage = "提交失败";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 根据id查询详情页数据
     *
     * @param id
     * @return
     */
    @SysLog("查询灾害风险点详情")
    @GetMapping("/queryData")
    public RestResponse queryData(String id) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            //获取业务表数据
            DisasterRiskEntity disasterRiskEntity = disasterRiskService.queryData(id);
            //获取附件
            if (PlatformObjectUtils.isEmpty(disasterRiskEntity.getFile())) {
                json.put("file", "");
            } else {
                List<AttachmentInfoEntity> attachmentInfoEntityList = attachmentInfoService.queryFile(disasterRiskEntity.getFile());
                json.put("file", attachmentInfoEntityList);
            }
            json.put("data", disasterRiskEntity);
            json.put("message", "查询成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 修改详情页数据
     *
     * @param disasterRiskEntity
     * @return
     */
    @SysLog("修改灾害风险点")
    @PostMapping("/modifyData")
    public RestResponse modifyData(@RequestBody DisasterRiskEntity disasterRiskEntity) {
        RestResponse response = null;
        try {
            boolean res = disasterRiskService.modifyData(disasterRiskEntity);
            if (res) {
                response = RestResponse.succeed("修改成功!");
                response.setMessage("修改成功!");
            } else {
                response = RestResponse.fail("修改失败!");
            }
        } catch (Exception e) {
            String errormessage = "修改失败!";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 修改并提交
     *
     * @param disasterRiskEntity
     * @return
     */
    @SysLog("修改并提交灾害风险点")
    @PostMapping("/modifyAndSubmit")
    public RestResponse modifyAndSubmit(@RequestBody DisasterRiskEntity disasterRiskEntity) {
        RestResponse response = null;
        try {
            boolean res = disasterRiskService.modifyAndSubmit(disasterRiskEntity);
            if (res) {
                response = RestResponse.succeed("修改并提交成功!");
                response.setMessage("修改并提交成功!");
            } else {
                response = RestResponse.fail("修改并提交失败!");
            }
        } catch (Exception e) {
            String errormessage = "修改并提交失败!";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 根据id删除数据
     *
     * @param
     * @return
     */
    @SysLog("删除灾害风险点")
    @GetMapping("/deleteDisasterRisk")
    public RestResponse deleteDisasterRisk(String ids) {
        RestResponse response = null;
        try {
            boolean res = disasterRiskService.deleteDisasterRisk(ids);
            if (res) {
                response = RestResponse.succeed("删除成功!");
                response.setMessage("删除成功!");
            } else {
                response = RestResponse.fail("删除失败!");
            }
        } catch (Exception e) {
            log.error("删除失败!", e);
            response = RestResponse.fail("删除失败!");
        }
        return response;
    }

    /**
     * 灾害风险点数据列表查询
     *
     * @param request
     * @param queryParams
     * @return
     */
    @SysLog("查询灾害风险点数据列表")
    @GetMapping("/queryDisasterRiskList")
    public RestResponse queryDisasterData(HttpServletRequest request, DisasterRiskParams queryParams) {
        RestResponse response = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> map = disasterRiskService.queryDisasterData(queryParams, curPage, pageSize);
            response = RestResponse.succeed(map);
        } catch (Exception e) {
            log.error("查询灾害风险点数据失败!", e);
            response = RestResponse.fail("查询灾害风险点数据失败!");
        }
        return response;
    }

    /**
     * 获取灾害风险点数据(用于pc端地图数据显示)
     *
     * @return
     */
    @GetMapping("/queryDisasterDataMap")
    public RestResponse queryDisasterDataMap() {
        RestResponse response = null;
        try {
            List<DisasterRiskEntity> disasterRiskEntities = disasterRiskService.queryDisasterDataMap();
            response = RestResponse.succeed(disasterRiskEntities);
            response.setMessage("获取灾害风险点数据成功");
        } catch (Exception e) {
            log.error("获取灾害风险点数据失败!", e);
            response = RestResponse.fail("获取灾害风险点数据失败!");
        }
        return response;
    }

    /**
     * 导入excel
     *
     * @param file
     * @return
     */
    @SysLog("导入灾害风险点数据")
    @PostMapping("/uploadDisasterRisk")
    public RestResponse uploadDisasterRisk(@RequestParam("file") MultipartFile file) {
        RestResponse response = null;
        try {
            Map<String, Object> map = disasterRiskService.uploadDisasterRisk(file);
            boolean res = ((Boolean) map.get("res")).booleanValue();
            ;
            if (res) {
                response = RestResponse.succeed("上传成功!");
                response.setMessage("上传成功!");
            } else {
                response = RestResponse.fail("上传失败!");
                response.setMessage(map.get("mess").toString());
            }
        } catch (ServiceException se) {
            log.error(se.getMessage(), se);
            return RestResponse.fail(se.getMessage());
        } catch (Exception e) {
            String errormessage = "上传失败!";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }


    /**
     * excel导出
     *
     * @param queryParams
     * @throws Exception
     */
    @SysLog("导出灾害风险点数据")
    @PostMapping("/exportDisasterRisk")
    public RestResponse exportDisasterRisk(HttpServletResponse response, DisasterRiskParams queryParams) {
        RestResponse restResponse = null;
        try {
            disasterRiskService.exportDisasterRisk(response, queryParams);
            RestResponse.succeed("导出成功!");
        } catch (Exception e) {
            String errormessage = "导出失败!";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


}